package vin.cookie.study;

import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class CookieDemo01
 */
public class CookieDemo01 extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public CookieDemo01() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 设置服务器端以UTF-8编码进行输出
		response.setCharacterEncoding("UTF-8");
		// 设置浏览器以UTF-8编码进行接收，解决中文乱码问题
		// or response.setHeader("content-type", "text/html;charset=UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		ServletOutputStream outputStream = response.getOutputStream();
		// 获取浏览器访问服务器时传递过来的cookie数组
		Cookie[] cookies = request.getCookies();
		// 如果用户是第一次访问，那么得到的cookies将是null
		if (cookies != null) {
			outputStream.write("您上次访问的时间是：".getBytes());
			for (int i = 0; i < cookies.length; i++) {
				Cookie cookie = cookies[i];

				if (cookie.getName().equals("lastAccessTime")) {
					Long lastAccessTime = Long.parseLong(cookie.getValue());
					Date date = new Date(lastAccessTime);
					SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					outputStream.write(simpleDateFormat.format(date).getBytes());
				}
			}

		} else {
			outputStream.write("这是您第一次访问本站！".getBytes());
		}
		// 用户访问过之后重新设置用户的访问时间，存储到cookie中，然后发送到客户端浏览器
		Cookie cookie = new Cookie("lastAccessTime", System.currentTimeMillis() + "");//// 创建一个cookie，cookie的名字是lastAccessTime
		// 设置Cookie的有效期为1天
		cookie.setMaxAge(24 * 60 * 60);

		// 将cookie对象添加到response对象中，这样服务器在输出response对象中的内容时就会把cookie也输出到客户端浏览器
		response.addCookie(cookie);

		// 存储中文
		cookie = new Cookie("name", URLEncoder.encode("张三", "UTF-8"));
		response.addCookie(cookie);

		response.getOutputStream().write(URLDecoder.decode(cookie.getValue(), "UTF-8").getBytes());// cookie.getValue());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
